package com.fan.test;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CountServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         //1.获取容器中计数器原本的次数
        ServletContext servletContext = getServletContext();
        Object count = servletContext.getAttribute("count");
        //判断Servletcontext中是否有计数器
        if (count==null){
            servletContext.setAttribute("count",1);
        }else {
            int number = ((int) count) + 1;
            servletContext.setAttribute("count",number);
        }
        //2.向客户算端响应welcome
        response.getWriter().write("Welcome!!!");
    }
}
